<?php
namespace Home\Controller;

use Common\Controller\BaseController;
use Common\Model\StaffModel;

class CheckController extends BaseController
{
    protected $action = '';
    protected $page_size = 10;
    protected $is_have = 2;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 4;
        $this->assign('block', self::$_action_block);
        if ($this->role_id == self::ROLE_TEACHER_MGR) {
            $this->is_have = 1;
        }
    }

    public function index()
    {
        $rooms = implode(',', $this->getRooms());
        $this->action = 3;
        $type = I('get.t', 0, 'intval');
        if(!empty($rooms)){
            $where = " check.room_id in({$rooms}) ";
        }else{
            $where = " check.room_id=0 ";
        }
        $fields = " check.id as id,check.tab_check_id,check.room_id croom_id,check.check_content,check.check_time,check.status,check.read,check.is_check,room.room_id as roomid,room.room_name ";
//    $join  = 'left join room on room.room_id=check.room_id left join staff on staff.staff_id=room.staff_id';
        $join = 'left join room on room.room_id=check.room_id';

        if (!empty($type)) {
            $where .= " and is_check={$type} ";
            $this->action = $type;
        }
//        dump($where);
//        exit;
        $checkModel = D('Check');
        $count = $checkModel
            ->join($join)
            ->where($where)
            ->count();
        vendor('Page');
        $Page = new \Vendor\Page($count, $this->page_size);
        $list = $checkModel->field($fields)
            ->join($join)
            ->where($where)
            ->order('check.id DESC')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
//    echo $checkModel->_sql();dump($list);exit;
        $this->assign('list', $list);

        $this->assign('page', $Page->show());
        $this->assign('type', $this->action);
        $this->display('index');
    }

    public function view()
    {
        $id = I('get.id', 0, 'intval');
        $type = I('get.t', 0, 'intval');

        $checkModel = D('Check');

        $where = "check.id={$id}";
        $field = "check.id,check.tab_check_id,check.check_content,check.check_time,check.status,check.remark,check.read,room.room_id,room.room_name ";
        $info = $checkModel->field($field)
            ->join('room on check.room_id=room.room_id', 'LEFT')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->where($where)
            ->order('check.id DESC')
            ->limit(1)
            ->select();
        if (empty($info)) {
            $this->error('数据不存在');
        }
//    dump($this->role_id);
//    dump(self::ROLE_TEACHER_MGR);
//    dump($this->is_have);
        $this->assign('info', $info[0]);
        $this->assign('is_have', $this->is_have);
        $view = 'view_' . $type;
        $this->display($view);
    }

    public function judge()
    {
        $status = I('post.status', 3, 'intval');
        $id = I('post.id', 0, 'intval');

        $checkModel = D('Check');
        $res = $checkModel->judge($id, $status, $remark);
        if (false !== $res) {
            $this->success('Success', U('Home/Check/index', array('t' => $status)));
        } else {
            $this->error('Error');
        }
    }

    public function getRooms()
    {
        $staffModel = new StaffModel();
        if ($this->role_id == self::ROLE_TEACHER_MGR) {
            $tmgr = $staffModel->getStaffByTmgrId($this->staff_id, 'rooms');
            $rooms = $this->returnRoom($tmgr);
        } else {
            $rooms = $staffModel->getStaffById($this->staff_id, 'rooms');
            $rooms = explode('|', $rooms['rooms']);
        }
        return $rooms;
    }

    public function returnRoom($tmgr)
    {

        $rooms = array();
        if (!empty($tmgr)) {
            foreach ($tmgr as $k=>$value) {
                $temp = explode('|', $value['rooms']);
                $rooms = array_merge($rooms,$temp);
            }
        }
        return $rooms;
    }
}